<?php

namespace app\data\controller\report;

use app\admin\service\WxWorkOAService;
use app\data\service\CityUserRelationService;
use think\admin\Controller;

/**
 * 数据统计-打卡统计
 * Class StatisticsCheckInRecord
 * @package app\data\controller\report
 */
class StatisticsCheckInRecord extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'data_record';

    /**
     * 打卡统计
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        // 设置页面标题
        $this->title = '打卡统计';

        // 获取城市师傅树
        $cityRelations = CityUserRelationService::instance()->getCityUserTree();

        // 查询企业微信打卡信息
        $checkInUsers = CityUserRelationService::instance()->checkInUsers($cityRelations);

        // 组装打卡数据
        foreach ($cityRelations as $cityRelation) {

            $realGroupPower = 0;
            $realGroupName = [];
            $checkInData = [
                'from_city' => $cityRelation['from_city'],
                'people_power' => count($cityRelation['users'] ?? []),
                'group_power' => count($cityRelation['users'] ?? []),
            ];

            foreach ($cityRelation['users'] ?? [] as $user) {
                if (in_array($user['user_id'], $checkInUsers)) {
                    $realGroupPower ++;
                    $realGroupName[] = $user['name'];
                }
            }

            $checkInData['real_group_power'] = $realGroupPower;
            $checkInData['real_group_name'] = implode('|', $realGroupName ?? []);
            $checkInList[] = $checkInData;
        }

        $this->checkInList = $checkInList ?? [];

        $this->fetch();
    }
}